define(function(require, exports, module){
	var ELView=require("elview");
	var aw=require("ajaxwrapper");
	var Form=require("form");
	var Subnav = require("subnav"); 
	var store =require("store");
	var Dialog=require("dialog");
	var Grid = require("grid");
	var activeUser = store.get("user");
	var enmu = require("enums");
	var template=require("./consumecard.tpl");
	require("./consumecard.css");
	var VerForm =require("form");
	
	//获取文件后缀名
    //输入"abc.jpg" 输出".jpg"
    function getFileExt(str){ 
	    var result = /\.[^\.]+$/.exec(str); 
	    return result; 
    }
	
	/*
	 * 检查输入的一串字符是否包含汉字
	 * 输入:str  字符串
	 * 返回:true 或 flase; true表示包含汉字
	 */
	function IsChinese(str)
	{
	 var reg=/^[\\u0391-\\uFFE5]+$/;
	 var result=reg.test(str);
	 return result;
	}
	/*
	 * 检查输入的一串字符是否是字符
	 * 输入:str  字符串
	 * 返回:true 或 flase; true表示为全部为字符 不包含汉字
	 */
	function checkStr(str){
	    if (/[^\w\.\/]/ig.test(str)) {
	        return false;
	    }
	    else {
	        return true;
	    }
	}
	var consumecard = ELView.extend({
		attrs:{
        	template:template
        },
		events:{
			"change #excelImport" : function(e){
				$("#fileName").val(e.target.files[0].name);
			},
        	"blur .J-form-consumecard-text-chipNumber" :function(e){
        		var form =this.get("form");
        		var pkCardManagement=form.getValue("pkCardManagement");
        		aw.ajax({
					url:"api/cate/consumecard/query",
					data:{
						chipNumber:form.getValue("chipNumber"),
						fetchProperties:"pkCardManagement,version,chipNumber,organization.pkOrganization"
					},
					dataType:"json",
					success:function(data){
							if(data.length>0&&data[0].pkCardManagement!=parseInt(pkCardManagement)){
							Dialog.alert({
	       						title:"提示",
	       						content:"芯片号重复,请修改!"
	       					});
							form.setValue("chipNumber","");
	       					return false;
						}
					}
				});
        	},
          	"blur .J-form-consumecard-text-cardNumber" :function(e){
        		var form =this.get("form");
        		var pkCardManagement=form.getValue("pkCardManagement");
        		aw.ajax({
					url:"api/cate/consumecard/query",
					data:{
						cardNumber:form.getValue("cardNumber"),
						fetchProperties:"pkCardManagement,version,organization.pkOrganization"
					},
					dataType:"json",
					success:function(data){
						if(data.length>0&&data[0].pkCardManagement!=parseInt(pkCardManagement)){
							Dialog.alert({
	       						title:"提示",
	       						content:"卡号重复,请修改!"
	       					});
							form.setValue("cardNumber","");
	       					return false;
						}
					}
				});
        	}
		},
		initComponent:function(params,widget){
			var subnav=new Subnav({
        		parentNode:".J-subnav",
				model : {
					title:"卡片管理",
					items : [{
						placeholder:"搜索",
						id : "search",
						type : "search",
						handler : function(str) {
							var g = widget.get("grid");
							aw.ajax({
								url:"api/cate/consumecard/search",
								data:{
									s:str,
									properties:"chipNumber,cardNumber",
									fetchProperties:"pkCardManagement,version,organization.name,organization.pkOrganization,"+
									"chipNumber,cardNumber,servicePoint.pkServicePoint,status.*,detial"
								},
								dataType:"json",
								success:function(data){
									g.setData(data);
								}
							});
						}
					},{
						id:"add",
						text:"新增",
						type:"button",
						handler:function(){
							var form = widget.get("form");
							widget.get("form").reset();
							widget.show([".J-form"]).hide([".J-grid"]);
							widget.get("subnav").hide(["add","search"]).show(["return"]);
						}
					},{
						id:"return",
						type:"button",
						text:"返回",
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-form"]);
							widget.get("subnav").hide(["return"]).show(["add","search"]);
						}
					}],
				}
        	});
        	this.set("subnav",subnav);      	
        	var grid=new Grid({
        		parentNode:".J-grid",
        		url : "api/cate/consumecard/query",
        		params : function() {
    				var subnav=widget.get("subnav");
    				return {
    					fetchProperties:"pkCardManagement,version,organization.name,organization.pkOrganization,"+
						"chipNumber,cardNumber,servicePoint.pkServicePoint,status.*,detial"
    				}
    			},
        		model : {
        			columns : [{
						name:"chipNumber",
						className:"chipNumber",
						label:"芯片号"
					},{
						name:"cardNumber",
						className:"cardNumber",
						label:"卡号"
					},{
        				name:"detial",
        				className:"detial",
        				label:"说明"
					},{
						name:"operate",
						className:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							key:"edit",
							icon:"icon-edit",
							handler:function(index,data,rowEle){
								var grid=widget.get("grid");
								var form = widget.get("form");
								form.reset();
								form.setData(data);
								form.setValue("servicePoint",data.servicePoint.pkServicePoint);
								form.setValue("status",data.status.key);
								
								widget.hide([".J-grid"]).show([".J-form"]);
								widget.get("subnav").hide(["add","search"]).show(["return"]);
							}
						},{
							key:"delete",
							icon:"icon-remove",
							handler:function(index,data,rowEle){
								aw.del("api/cate/consumecard/"+data.pkCardManagement+"/delete",function() {
									grid.refresh();
								});	
							}
						}]
					}]
    			}
    		 });
    		 this.set("grid",grid);
    		 
        	 var form=new Form({
        		 show:false,
        		 parentNode:".J-form",
	  			 model:{
	  				 saveaction : function() {
	          			var form = widget.get("form");
	           			var data=$("#consumecard").serialize();
	           			var chipNumber=form.getValue("chipNumber");
	           			var cardNumber=form.getValue("cardNumber");
	           			var chipHexNumber = chipNumber.toString(16).toUpperCase();
	           			data += "&chipHexNumber=" + chipHexNumber;
	           			aw.saveOrUpdate("api/cate/consumecard/save",data,function(data){
	           				widget.hide([".J-form"]).show([".J-grid"]);
	      					widget.get("subnav").show(["add","search"]).hide(["return"]);
	      					widget.get("grid").refresh();
	      				});
	      			 },
	      			 cancelaction:function(){
	      				 	widget.show([".J-grid"]).hide([".J-form"]);
	      				 	widget.get("subnav").show(["add","search"]).hide(["return"]);
	      					return false;
	      			 },
	 				id:"consumecard",
					items:[{
							name:"pkCardManagement",
							type:"hidden",
						},{
							name : "version",
							defaultValue : "0",
							type : "hidden"
						},{
							name:"servicePoint",
							defaultValue:activeUser.servicePoint.pkServicePoint,
							type:"hidden",
						},{
							name:"status",
							defaultValue:"Free",
							type:"hidden",
						},{
							name:"chipNumber",
							label:"芯片号",
							validate:["required"],
							exValidate: function(value){
								if(checkStr(value)==false){
									return "芯片号不能有中文字符";
								}else{
									return true;
								}
							}
						},{
							name:"cardNumber",
							label:"卡号",
							validate:["required"],
							exValidate: function(value){
								if(checkStr(value)==false){
									return "卡号不能有中文字符";
								}else{
									return true;
								}
							}
						},{
							name:"detial",
							label:"说明",
							type:"textarea",
							height:200,
							exValidate: function(value){
								if(value.length>200){
									return "描述不能超过200个字";
								}else{
									return true;
								}
							}
						}]
			    }
        	 });
        	 this.set("form",form);
		}
	});
	module.exports = consumecard;	
});
